package cn.e4j.modular.api.controller;

import cn.digiwin.wx.utils.WeixinHandler;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.service.GwaService;
import cn.e4j.modular.api.service.Gwat100Service;
import cn.e4j.modular.api.service.Gwat210Service;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.crm.entity.GwaaT;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 意向商机指派查询确认控制类
 * Created by wenchuan on 2018/4/27.
 */
@RestController
@RequestMapping("api/wx/gwat210")
public class Gwat210Controller extends ControllerSupport {
    @Autowired
    Gwat210Service gwat210Service;

    @Autowired
    Gwat100Service gwat100Service;

    @Autowired
    GwaService gwaService;

    /**
     * 获取潜在商机意向订单信息
     *
     * @return
     */
    @RequestMapping(value = "getGwcoDatas")
    public ResponseData getGwcoDatas(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<>();
        SysUser user = wapper.getSysUser();
        Dto gwaaDtoIn = new Dto();
        try {
            gwaaDtoIn.put("GWAAENT", inDto.getString("ENT"));
            gwaaDtoIn.put("GWAA001", user.getACCOUNT());
            QueryWrapper<GwaaT> wrapper = new QueryWrapper<>();
            wrapper.allEq(gwaaDtoIn);
            GwaaT gwaaT = new GwaaT().selectOne(wrapper);
            inDto.put("GWAA001", user.getACCOUNT());
            inDto.put("GWAA003", user.getACCOUNT());
            inDto.put("GWCOENT", inDto.getString("ENT"));
            //List<Dto> dto = gwat210Service.getGwaaData(inDto);
            if (gwaaT.getGWAA007().equals("0")) { //业务人员
                list = gwat210Service.getGwco1(wapper.getPage(),inDto);
            } else if (gwaaT.getGWAA007().equals("1")) { //经销商
                list = gwat210Service.getGwco2(wapper.getPage(),inDto);
            }
        } catch (Exception e) {
           e.printStackTrace();
           return ERROR;
        }
        return SUCCESS(list);
    }

    /**
     * 判断是否在gwcq中有数据
     *
     * @return
     */
    @RequestMapping(value = "getGwcq")
    public ResponseData getGwcq(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = gwat210Service.getGwcq(inDto);
        return SUCCESS(list);
    }

    /**
     * 查询潜在订单的状态GWCOACTI
     *
     * @return
     */
    @RequestMapping(value = "getGWCOACTI")
    public ResponseData getGWCOACTI(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = gwat210Service.getGwcoacti(inDto);
        return SUCCESS(list);
    }

    /**
     * 修改gwco_t的状态
     *
     * @return
     */
    @RequestMapping(value = "updateData")
    public ResponseData updateData(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        Integer appCode=200;
        try {
            inDto.put("GWCOENT", inDto.getString("ENT"));
            gwat210Service.update(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            appCode=500;
        }

        if (appCode==200) {
            try {
                String _GWCOACTI = inDto.getString("GWCOACTI");

                Dto gwaaDto = new Dto();
                gwaaDto.put("GWAAENT", inDto.getString("ENT"));
                gwaaDto.put("MMRE004", inDto.getString("_GWCO026"));
                List<Dto> sysUser = gwat100Service.getGwaa(gwaaDto);

                String _GWCO007 = inDto.getString("_GWCO007");
                if (ToolUtil.isNotEmpty(_GWCO007)) {
                    gwaaDto.put("GWAAACTI", "Y");
                    gwaaDto.put("GWAA004", _GWCO007);
                    List<Dto> sysUser1 = gwaService.getGwaUserByMobile(gwaaDto);
                    if (null != sysUser1) {
                        // 添加的时候 是否需要判断用户重复性？
                        sysUser.addAll(sysUser1);
                    }
                }

                WeixinHandler.sendMsg(sysUser,
                        String.format("意向商机审核[%s]！经销商[%s], 负责人[%s], 手机号[%s]",
                                _GWCOACTI.equals("Y") ? "审核通过" : _GWCOACTI.equals("N") ? "取消审核" : _GWCOACTI.equals("X") ? "作废" : "",
                                inDto.getString("_GWAKL003"),
                                inDto.getString("_GWCO012"),
                                inDto.getString("_GWCO013")
                        ));
            } catch (Exception e) {
                e.printStackTrace();
                return ERROR;
            }
        }

        if (appCode == 500)
            return ERROR("修改出现未知错误!");

        return SUCCESS;
    }


    /**
     * 批量修改状态
     *
     * @return
     */
    @RequestMapping("update")
    public ResponseData update(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        JSONObject inJson;
        JSONArray characteristics = new JSONArray();
        Integer appCode=200;
        try {
            inJson = JSONObject.parseObject(inDto.getString("obj"));
            characteristics = inJson.getJSONArray("CHARACTERISTICS");
            for (int i = 0; i < characteristics.size(); i++) {
                JSONObject item = characteristics.getJSONObject(i);
                Dto dto = new Dto();
                dto.put("GWCOACTI", item.getString("GWCOACTI"));
                dto.put("GWCODOCNO", item.getString("GWCODOCNO"));
                dto.put("GWCOENT", inDto.getString("ENT"));
                gwat210Service.update(dto);
            }
        } catch (Exception e) {
            e.printStackTrace();
            appCode=500;
        }

        if (appCode == 200){
            try {
                for (int i = 0; i < characteristics.size(); i++) {
                    JSONObject item = characteristics.getJSONObject(i);
                    String _GWCOACTI = item.getString("GWCOACTI");

                    Dto gwaaDto = new Dto();
                    gwaaDto.put("GWAAENT", inDto.getString("ENT"));
                    gwaaDto.put("MMRE004", item.getString("_GWCO026"));
                    List<Dto> sysUser = gwat100Service.getGwaa(gwaaDto);

                    String _GWCO007 = item.getString("_GWCO007");
                    if (ToolUtil.isNotEmpty(_GWCO007)) {
                        gwaaDto.put("GWAAACTI", "Y");
                        gwaaDto.put("GWAA004", _GWCO007);
                        List<Dto> sysUser1 = gwaService.getGwaUserByMobile(gwaaDto);
                        if (null != sysUser1) {
                            // 添加的时候 是否需要判断用户重复性？
                            sysUser.addAll(sysUser1);
                        }
                    }

                    WeixinHandler.sendMsg(sysUser,
                            String.format("意向商机审核[%s]！经销商[%s], 负责人[%s], 手机号[%s]",
                                    _GWCOACTI.equals("Y") ? "审核通过" : _GWCOACTI.equals("N") ? "取消审核" : _GWCOACTI.equals("X") ? "作废" : "",
                                    item.getString("_GWAKL003"),
                                    item.getString("_GWCO012"),
                                    item.getString("_GWCO013")
                            ));
                }

            } catch (Exception e) {
                e.printStackTrace();
                return ERROR;
            }
        }

        if (appCode == 500)
            return ERROR("修改时出现未知异常");

        return SUCCESS;
    }

}
